package com.cqwshzj.util;/**
 * @Description:
 * @Auther: Huang Zhijin
 * @Version: 1.0.0
 * @Date: 2021/4/9 11:22
 */

import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;

/**
 *
 * 〈〉
 *
 * @author Huang Zhijin
 * @create 2021/4/9
 * @since 1.0.0
 */
public class ElasticJobUtil {

	/**
	 * 获取 LiteJobConfiguration
	 * @param jobClass
	 * @param cron
	 * @param shardingTotalCount
	 * @param shardingItemParameters
	 * @return
	 */
	public static LiteJobConfiguration getLiteJobConfiguration(
			final String jobName,
			final Class<? extends SimpleJob> jobClass,
  		    final String cron,
			final int shardingTotalCount,
			final String shardingItemParameters) {

		final JobCoreConfiguration jobCoreConfiguration = JobCoreConfiguration.newBuilder( jobName, cron, shardingTotalCount)
				.shardingItemParameters(shardingItemParameters)
				.build();

		final SimpleJobConfiguration simpleJobConfiguration = new SimpleJobConfiguration(jobCoreConfiguration, jobClass.getCanonicalName());
		return LiteJobConfiguration.newBuilder(simpleJobConfiguration)
				.overwrite(true)
				.build();
	}

	/**
	 *
	 * @param jobName
	 * @param simpleJob
	 * @param cron
	 * @param shardingTotalCount
	 * @param shardingItemParameters
	 * @param registryCenter
	 * @return
	 */
	public static SpringJobScheduler createSpringJobScheduler(
			final String jobName,
			final SimpleJob simpleJob,
			final String cron,
			final int shardingTotalCount,
			final String shardingItemParameters,
			final CoordinatorRegistryCenter registryCenter
		){
		LiteJobConfiguration liteJobConfiguration = getLiteJobConfiguration( jobName, simpleJob.getClass(), cron, shardingTotalCount, shardingItemParameters );
		return new SpringJobScheduler(simpleJob, registryCenter, liteJobConfiguration );
	}


}
